import MinRequest from '../utils/http.js'

const minRequest = new MinRequest()

function checkSuccess(data) {

	if (data instanceof ArrayBuffer && typeof data === 'string') {
		return data;
	}
	if (data.statusCode >= 200 && data.statusCode < 300) {
		
		return data.data;
	}
	if (data.statusCode === 401) {
		uni.redirectTo({
			url:'/pages/login/login',
			success:()=>{
				setTimeout(() =>{
					uni.showToast({
						icon: 'none',
						title: '检测到你未登录, 请先登录在使用小程序',
					})
				}, 1000);
			}
			
		})
		
		return 
	}
	if (data.statusCode === 402) {
		uni.redirectTo({
			url:'/pages/login/index',
			success:()=>{
				setTimeout(() =>{
					uni.showToast({
						icon: 'none',
						title: '检测到你未绑定手机, 请先绑定手机',
					})
				}, 1000);
			}
			
		})
		
		return 
	}
	uni.showToast({
		icon: 'none',
		title: '服务器异常',
	});
}
// 请求拦截器
minRequest.interceptors.request((request) => {
	let token = uni.getStorageSync('token')
	request.header.token = `${token}`
	// #ifdef H5
	
	request.header.token = `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJhdWQiOiIiLCJpYXQiOjE2MjIxODcxMDIsIm5iZiI6MTYyMjE4NzEwMiwiZXhwIjoxNjI0Nzc5MTAyLCJvcGVuaWQiOiJvc1dpMDRtSGRFVFBPaFdld29qU1R6djd2N0JZIn0.XE57iap71U5JY-q7p7WFcDlZDTzxPal6k1yIWeUmJck`
	// #endif
	return request
})

// 响应拦截器
minRequest.interceptors.response((response) => {
	return checkSuccess(response)
})

// 设置默认配置
minRequest.setConfig((config) => {
	config.baseURL = 'https://boss.daxinxin.vip/boss/'
	return config
})

export default {
	apis: {
		login(data) {
			return minRequest.get('/WxLogin', data)
		},
		bd(data) {
			return minRequest.post('Login', data)
		},
		approvalList(data) {
			return minRequest.get('approvalList', data)
		},
		checkApproval(data) {
			return minRequest.post('checkApproval', data)
		},
		checkApproval(data) {
			return minRequest.post('checkApproval', data)
		},
		checkApprovalGet(data) {
			return minRequest.get('checkApproval', data)
		},
		staff(data) {
			return minRequest.post('staff', data)
		},
		ssgeList(data) {
			return minRequest.post('ssgeList', data)
		},
		statement(data) {
			return minRequest.post('statement', data)
		},
		revenue(data) {
			return minRequest.post('revenue', data)
		},
		client(data) {
			return minRequest.post('client', data)
		},
		mywork(data) {
			return minRequest.post('mywork', data)
		},	
		sms(data) {
			return minRequest.post('sms', data)
		},
        statement(data){
		return minRequest.post('statement', data)
		}

	}
}
